<script setup lang="ts">
import { ref, onMounted, nextTick } from 'vue'
import { Document, Check, User, List, Plus, Upload, Download, CaretTop } from '@element-plus/icons-vue'
import * as echarts from 'echarts'

// 分类选择值
const value = ref('all')

// 统计数据
const stats = ref([
  { 
    title: '今日生成量', 
    value: '1,284', 
    icon: Document, 
    color: '#409EFF',
    trend: '较昨日增长 12.5%',
    trendUp: true
  },
  { 
    title: '模型成功率', 
    value: '98.6%', 
    icon: Check, 
    color: '#67C23A',
    trend: '较上周提升 0.8%',
    trendUp: true
  },
  { 
    title: '活跃用户数', 
    value: '3,721', 
    icon: User, 
    color: '#409EFF',
    trend: '较昨日增长 5.2%',
    trendUp: true
  },
  { 
    title: '素材库总量', 
    value: '42,891', 
    icon: List, 
    color: '#909399',
    trend: '更新于 10 分钟前',
    trendUp: false
  }
])

// 素材库数据
const materials = ref([
  { id: 'SP202311001', uploader: '赵明杰', tags: ['高品质', '风景'], createTime: '2023-11-20 15:30:21' },
  { id: 'SP202311002', uploader: '林思敏', tags: ['动物园', '萌宠'], createTime: '2023-11-20 14:25:16' },
  { id: 'SP202311003', uploader: '张云帆', tags: ['人物照', '写实'], createTime: '2023-11-20 11:42:53' }
])

// 分页数据
const pagination = ref({
  currentPage: 1,
  pageSize: 10,
  total: 42891
})

// 图表数据
const productionTrendOptions = {
  xAxis: {
    type: 'category',
    data: ['11-14', '11-15', '11-16', '11-17', '11-18', '11-19', '11-20'],
    axisLine: {
      lineStyle: {
        color: '#E0E6F1'
      }
    },
    axisLabel: {
      color: '#909399'
    }
  },
  yAxis: {
    type: 'value',
    axisLine: {
      show: false
    },
    axisTick: {
      show: false
    },
    splitLine: {
      lineStyle: {
        color: '#EBEEF5'
      }
    },
    axisLabel: {
      color: '#909399'
    }
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    top: '10%',
    containLabel: true
  },
  tooltip: {
    trigger: 'axis'
  },
  series: [
    {
      data: [850, 900, 900, 950, 1250, 1300, 1250],
      type: 'line',
      smooth: true,
      symbol: 'circle',
      symbolSize: 6,
      lineStyle: {
        color: '#409EFF',
        width: 3
      },
      itemStyle: {
        color: '#409EFF',
        borderColor: '#fff',
        borderWidth: 2
      },
      areaStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            {
              offset: 0,
              color: 'rgba(64, 158, 255, 0.3)'
            },
            {
              offset: 1,
              color: 'rgba(64, 158, 255, 0.1)'
            }
          ]
        }
      }
    }
  ]
}

const modelSuccessRateOptions = {
  xAxis: {
    type: 'category',
    data: ['11-14', '11-15', '11-16', '11-17', '11-18', '11-19', '11-20'],
    axisLine: {
      lineStyle: {
        color: '#E0E6F1'
      }
    },
    axisLabel: {
      color: '#909399'
    }
  },
  yAxis: {
    type: 'value',
    min: 90,
    max: 100,
    axisLine: {
      show: false
    },
    axisTick: {
      show: false
    },
    splitLine: {
      lineStyle: {
        color: '#EBEEF5'
      }
    },
    axisLabel: {
      color: '#909399'
    }
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    top: '10%',
    containLabel: true
  },
  tooltip: {
    trigger: 'axis'
  },
  series: [
    {
      data: [96, 97, 97.5, 98, 98.2, 98.5, 98.6],
      type: 'line',
      smooth: true,
      symbol: 'circle',
      symbolSize: 6,
      lineStyle: {
        color: '#67C23A',
        width: 3
      },
      itemStyle: {
        color: '#67C23A',
        borderColor: '#fff',
        borderWidth: 2
      }
    }
  ]
}

// 初始化图表
onMounted(() => {
  nextTick(() => {
    // 初始化生成量趋势图表
    const productionTrendChart = echarts.init(document.getElementById('productionTrend'))
    productionTrendChart.setOption(productionTrendOptions)
    
    // 初始化模型成功率图表
    const modelSuccessRateChart = echarts.init(document.getElementById('modelSuccessRate'))
    modelSuccessRateChart.setOption(modelSuccessRateOptions)
    
    // 窗口大小变化时重绘图表
    window.addEventListener('resize', () => {
      productionTrendChart.resize()
      modelSuccessRateChart.resize()
    })
  })
})
</script>

<template>
  <div class="dashboard-container">
    <!-- 统计卡片 -->
    <div class="stats-cards">
      <el-card v-for="(stat, index) in stats" :key="index" class="stat-card" shadow="hover">
        <div class="stat-content">
          <div class="stat-info">
            <h3>{{ stat.title }}</h3>
            <div class="stat-value">{{ stat.value }}</div>
            <div class="stat-trend" :class="{ 'trend-up': stat.trendUp }">
              <el-icon v-if="stat.trendUp"><CaretTop /></el-icon>
              {{ stat.trend }}
            </div>
          </div>
          <div class="stat-icon" :style="{ backgroundColor: stat.color + '15' }">
            <el-icon :style="{ color: stat.color }"><component :is="stat.icon" /></el-icon>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 素材库总览 -->
    <el-card class="material-overview" shadow="hover">
      <template #header>
        <div class="card-header">
          <h3>素材库总览</h3>
        </div>
      </template>
      
      <div class="material-actions">
        <div class="left-actions">
          <el-button type="primary">
            <el-icon><Plus /></el-icon> 新增素材
          </el-button>
          <el-button>
            <el-icon><Upload /></el-icon> 批量导入
          </el-button>
          <el-button>
            <el-icon><Download /></el-icon> 批量导出
          </el-button>
        </div>
        <div class="right-actions">
          <el-select v-model="value" placeholder="全部分类" size="default">
            <el-option label="全部分类" value="all" />
            <el-option label="图片素材" value="image" />
            <el-option label="文本素材" value="text" />
            <el-option label="视频素材" value="video" />
          </el-select>
        </div>
      </div>

      <el-table :data="materials" style="width: 100%">
        <el-table-column prop="id" label="素材ID" width="180" />
        <el-table-column prop="uploader" label="上传者" width="180" />
        <el-table-column label="标签">
          <template #default="scope">
            <el-tag v-for="(tag, index) in scope.row.tags" :key="index" 
                   :type="index === 0 ? 'primary' : 'success'" 
                   size="small" 
                   class="material-tag">
              {{ tag }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="180">
          <template #default>
            <el-button link type="primary" size="small">查看</el-button>
            <el-button link type="danger" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <span class="total-count">共 {{ pagination.total }} 条记录</span>
        <el-pagination
          background
          layout="prev, pager, next"
          :total="pagination.total"
          :page-size="pagination.pageSize"
          :current-page="pagination.currentPage"
        />
      </div>
    </el-card>

    <!-- 图表区域 -->
    <div class="chart-container">
      <el-card class="chart-card" shadow="hover">
        <template #header>
          <div class="card-header">
            <h3>生成量趋势</h3>
          </div>
        </template>
        <div class="chart" id="productionTrend"></div>
      </el-card>

      <el-card class="chart-card" shadow="hover">
        <template #header>
          <div class="card-header">
            <h3>模型成功率</h3>
          </div>
        </template>
        <div class="chart" id="modelSuccessRate"></div>
      </el-card>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.dashboard-container {
  .stats-cards {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
    margin-bottom: 20px;

    .stat-card {
      border-radius: 8px;
      
      .stat-content {
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        .stat-info {
          h3 {
            font-size: 14px;
            color: #909399;
            margin: 0 0 10px 0;
            font-weight: normal;
          }
          
          .stat-value {
            font-size: 24px;
            font-weight: bold;
            color: #303133;
            margin-bottom: 5px;
          }
          
          .stat-trend {
            font-size: 12px;
            color: #909399;
            display: flex;
            align-items: center;
            
            &.trend-up {
              color: #67C23A;
            }
            
            .el-icon {
              margin-right: 4px;
            }
          }
        }
        
        .stat-icon {
          width: 48px;
          height: 48px;
          border-radius: 8px;
          display: flex;
          align-items: center;
          justify-content: center;
          
          .el-icon {
            font-size: 24px;
          }
        }
      }
    }
  }

  .material-overview {
    margin-bottom: 20px;
    border-radius: 8px;
    
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      
      h3 {
        font-size: 16px;
        margin: 0;
        font-weight: 600;
      }
    }
    
    .material-actions {
      display: flex;
      justify-content: space-between;
      margin-bottom: 20px;
      
      .left-actions {
        display: flex;
        gap: 10px;
      }
    }
    
    .material-tag {
      margin-right: 5px;
    }
    
    .pagination-container {
      margin-top: 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      
      .total-count {
        color: #909399;
        font-size: 14px;
      }
    }
  }

  .chart-container {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    
    .chart-card {
      border-radius: 8px;
      
      .card-header {
        h3 {
          font-size: 16px;
          margin: 0;
          font-weight: 600;
        }
      }
      
      .chart {
        height: 300px;
      }
    }
  }
}
</style>